#ifndef IMAGESEGMENT_H
#define IMAGESEGMENT_H

class Image;

class ImageSegment
{
public:
	ImageSegment();
	~ImageSegment();

	void SetImage(Image* parentimage);
	void ClearImage();

	unsigned int OriginX() const;
	unsigned int OriginY() const;
	void IncrementOriginX(signed int incx);
	void IncrementOriginY(signed int incy);
	void SetOriginX(unsigned int orgx);
	void SetOriginY(unsigned int orgy);
	void SetDimension(unsigned int newWidth, unsigned int newHeight);

	void ImportRaw(unsigned char *data, unsigned int size);
	void ExportRaw(unsigned char *data, unsigned int size) const;

	unsigned int Width() const;
	unsigned int Height() const;
	unsigned int Size() const;

	void Substract(const ImageSegment& subseg);
	void Add(const ImageSegment& subseg);

	unsigned char GetComponent(unsigned int x, unsigned int y) const;
	void SetComponent(unsigned int x, unsigned int y, unsigned char intensity);

	void ReadRawFromSegment(const ImageSegment& seg);
	void ReadRawFromImage(Image& im);

	unsigned int MAD(const ImageSegment& reference);
	ImageSegment* LocateSegment(Image& reference, unsigned int windowsize);
	ImageSegment* LocateSegmentLog(Image& reference, unsigned int windowsize);

protected:

private:
	Image *image;
	unsigned int originx, originy;
	unsigned int width, height;

	bool PointInImage(signed int x, signed int y, const Image& rect);
	bool SegmentInImage(signed int x, signed int y, const Image& rect);
	void FitPointInImage(signed int *x, signed int *y, const Image& rect);
};

#endif //IMAGESEGMENT_H
